package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.core.SysActivity;
import com.edu.app.bean.core.SysUser;
import com.edu.app.bean.linkbeans.SysUserActivity;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.linkservices.SysUserActivityService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/user_activity")
@RestController
public class SysUserActivityController {

    @Autowired
    private SysUserActivityService sysUserActivityService;

    //使用get需要添加默认get函数
    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getUserActivityList(){
        List<SysUserActivity> sysUserActivityList = sysUserActivityService.getObjectList();
        return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, StaticConstant.USER_ACTIVITY_LIST, sysUserActivityList);
    }

    @GetMapping(value = "/getActivityByUserId")
    @RequiresAuthentication
    public ModelMap getActivityByUserId(@RequestParam Integer id){
        List<SysActivity> sysActivityList = sysUserActivityService.getActivityByUserId(id);
        return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, StaticConstant.ACTIVITY_LIST, sysActivityList);
    }

    @GetMapping(value = "/getUserByActivityId")
    @RequiresAuthentication
    public ModelMap getUserByActivityId(@RequestParam Integer id){
        List<SysUser> sysUserList = sysUserActivityService.getUserByActivityId(id);
        return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, StaticConstant.USER_LIST, sysUserList);
    }

    //for calendar purpose, to get the my activities by date
    @GetMapping(value = "/getMyActivityByDate")
    @RequiresAuthentication
    public ModelMap getMyActivityByDate(@RequestHeader("Authorization") String token, @RequestParam String date){
        try{
            String username = JWTUtil.getUsername(token);
            List<SysActivity> sysActivityList = sysUserActivityService.getActivityByUserName(username, date);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, StaticConstant.ACTIVITY_LIST, sysActivityList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    //To get the date which has activities, to show the red dot for those days
    @GetMapping(value = "/getDateHasActivity")
    @RequiresAuthentication
    public ModelMap getDateHasActivity(@RequestHeader("Authorization") String token, @RequestParam String rolePlay){
        try{
            String username = JWTUtil.getUsername(token);
            List<String> dateList = sysUserActivityService.getDateHasActivity(username, rolePlay);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "date_list", dateList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getInComingActivityModel")
    @RequiresAuthentication
    public ModelMap getInComingActivityModel(@RequestHeader("Authorization") String token, @RequestParam String role, @RequestParam Integer offset, @RequestParam Integer limit, @RequestParam String type){
        try{
            String username = JWTUtil.getUsername(token);
            type = "%" + type + "%";
            PageInfo pageInfo = sysUserActivityService.getInComingActivityModel(username, role, offset, limit, type);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getActivityModelByStatus")
    @RequiresAuthentication
    public ModelMap getActivityModelByStatus(@RequestHeader("Authorization") String token, @RequestParam Integer status, @RequestParam String role, @RequestParam String type, @RequestParam Integer offset, @RequestParam Integer limit, @RequestParam String seq){
        try{
            String username = JWTUtil.getUsername(token);
            type = "%" + type + "%";
            PageInfo pageInfo = sysUserActivityService.getActivityModelByStatus(username, status, role, type, offset, limit, seq);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getMyIncomingActivity")
    @RequiresAuthentication
    public ModelMap getMyIncomingActivity(@RequestHeader("Authorization") String token, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysUserActivityService.getUserActivityByPage(username, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getMyPendingConfirmActivity")
    @RequiresAuthentication
    public ModelMap getMyPendingConfirmActivity(@RequestHeader("Authorization") String token, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysUserActivityService.getUserPendingActivityByPage(username, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getMyPendingFeedbackActivity")
    @RequiresAuthentication
    public ModelMap getMyPendingFeedbackActivity(@RequestHeader("Authorization") String token, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysUserActivityService.getMyPendingFeedbackActivity(username, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getMyCompletedActivity")
    @RequiresAuthentication
    public ModelMap getMyCompletedActivity(@RequestHeader("Authorization") String token, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysUserActivityService.getUserCompletedActivityByPage(username, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    /*
     * 为User添加角色
     * http://localhost:8080/api/user_roles?user_id=1&role_id=1
     * */
    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addUserActivity(@RequestHeader("Authorization") String token, @RequestParam Integer user_id, @RequestParam Integer activity_id){

        try{
            SysUserActivity sysUserActivity = new SysUserActivity(user_id, activity_id);
            sysUserActivity.initializeSystemColumns(JWTUtil.getUsername(token));
            Integer r = sysUserActivityService.add(sysUserActivity);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_USER_ACTIVITY_SUCCEED, "activity_id" ,sysUserActivity.getId());
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.USER_ACTIVITY_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    /*
     * 删除用户角色
     * http://localhost:8080/api/deleteUserRole?user_id=1&role_id=1
     * */
    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap deleteUserActivity(@RequestParam Integer user_id, @RequestParam Integer activity_id){

        SysUserActivity sysUserActivity = sysUserActivityService.getUserActivity(user_id, activity_id);

        if(sysUserActivity != null){
            try{
                Integer r = sysUserActivityService.delete(sysUserActivity.getId());
                if(r > 0){
                    return MsgUtil.retMsg(SysMessage.DELETE_USER_ACTIVITY_SUCCEED, null ,null);
                }else{
                    return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
                }
            }catch (Exception e){
                return ErrorUtil.retErrMsg(e);
            }
        }else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }
}
